# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import redis
import json
from scrapy import Item


class BitcoinCrawlerPipeline:

    def __init__(self, settings):
        redis_host = settings.get('REDIS_HOST', '127.0.0.1')
        redis_port = settings.get('REDIS_PORT', 6379)
        redis_db = settings.get('REDIS_DB_INDEX', 0)
        self.redis_conn = None
        self.redis_pool = redis.ConnectionPool(host=redis_host, port=redis_port, db=redis_db)

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings)

    def open_spider(self, spider):
        self.redis_conn = redis.Redis(connection_pool=self.redis_pool)

    def close_spider(self, spider):
        self.redis_conn.connection_pool.disconnect()

    def process_item(self, item, spider):
        self.insert_db(item)
        return item

    def insert_db(self, item):
        if isinstance(item, Item):
            item = dict(item)
        self.redis_conn.rpush('{coin_name}_list'.format(coin_name=item['coin_name']), json.dumps(item))
